<template>
    <main-wrap>
        <div class="rmfbox">
            <van-form >
                <van-cell-group inset>
                    <van-field v-model="formData.remark" name="备注名" label="备注名" placeholder="请输入备注名" show-word-limit maxlength="20"/>
                    <van-field v-model="formData.remark_company" name="企业" label="企业名" placeholder="请输入企业名" show-word-limit maxlength="20"/>
                    <van-field v-model="remark_mobiles" name="电话" label="电话" placeholder="请输入电话"/>
                    <van-field v-model="formData.description" name="描述" label="描述" placeholder="请输入描述" rows="5" type="textarea" show-word-limit maxlength="150"/>
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" color="#e3af2f" @click="onSubmit">提交</van-button>
                </div>
            </van-form>
        </div>
    </main-wrap>
</template>
<script setup>
import { ref,onMounted } from "vue";
import {useRouter} from 'vue-router'
import {getCustomList,updateRemark} from '@/api/qy-wechat-api'
import { useUserStore } from '@/stores/modules/user'
import mainWrap from '@/components/mainWrap.vue'
import {toast} from '@/utils/vant'

const router = useRouter()
const userStore = useUserStore()

const external_userid = ref('') // 外部联系人id
const member_userid = ref('') // 内部成员id
const cDetail = ref({}) //客户详情
const formData = ref({})
const remark_mobiles = ref('')

onMounted(()=>{
    const {currentRoute} = router
    external_userid.value = currentRoute.value.query.cuid
    member_userid.value = currentRoute.value.query.muid // userStore.userInfo.userid
    getCustomList({
        pageSize:10,
        pageIndex:1,
        external_userid:external_userid.value,
        'follow_info.userid':member_userid.value
    }).then(res =>{
        cDetail.value = res.data.data[0]

        // 赋值
        const dt = res.data.data[0]
        formData.value.remark =  dt.remark || dt.name
        remark_mobiles.value = dt.remark_mobiles?dt.remark_mobiles.join(','):'' // 备注电话
        formData.value.remark_company = dt.remark_corp_name // 备注企业
        formData.value.description = dt.description // 备注描述
    })
})

const onSubmit = async ()=>{
    if(userStore.userInfo.userid!=member_userid.value){
        return toast('不是你的客户，不能修改！')
    }
    remark_mobiles.value && (formData.value.remark_mobiles = [remark_mobiles.value])
    const res = await updateRemark({
        userid:member_userid.value,
        external_userid:external_userid.value,
        ...formData.value
    })
    if(res.code == 200){
        toast('保存成功')
        setTimeout(() => {
            router.back()
        }, 2000);
    }else{
        toast(res.message || '出错了')
    }
}
</script>

<style lang="scss" scoped>
.rmfbox{
    padding: 40px 0 0 0;
}
</style>